//
//  JZBAddCountViewController.m
//  JZB
//
//  Created by liujianlong on 2022/8/31.
//

#import "JZBAddCountViewController.h"
#import "SWPrivacyViewController.h"

@interface JZBAddCountViewController ()
@property (strong, nonatomic) IBOutlet JZBTextField *input1;
@property (strong, nonatomic) IBOutlet JZBTextField *input2;
@property (strong, nonatomic) IBOutlet UIView *tool;

@end

@implementation JZBAddCountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    
    if (self.type == AddCountTypeNew) {
        self.title = LS(@"创建账户");
        self.input1.placeholder = LS(@"请输入账号");
        self.input2.placeholder = LS(@"请设置密码");
    }else{
        self.title = LS(@"忘记密码");
        self.tool.hidden = YES;
        self.input1.placeholder = LS(@"请设置登录密码");
        self.input2.placeholder = LS(@"确认登录密码");
    }
    
    self.input2.secureTextEntry = YES;
    self.input1.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.input1.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]}];
    self.input2.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.input2.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]}];
    self.input2.secureTextEntry = YES;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    btn.tag = 204;
    [btn setImage:[UIImage imageNamed:@"查看"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(actionForBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.input2.rightViewMode = UITextFieldViewModeAlways;
    self.input2.rightView = btn;
}

- (IBAction)actionForBtn:(UIButton *)sender {
    switch (sender.tag) {
        case 201:///注册
        {
            if (self.input1.text.length <= 0) {
                return [RLHud msg:LS(@"账号不能为空")];
            }else if (self.input2.text.length <= 0){
                return [RLHud msg:LS(@"密码不能为空")];
            }else if ([self.input1.text isNumberOrAlphabet] == NO){
                return [RLHud msg:LS(@"账号只能包含数字和字母")];
            }else if ([self.input2.text isNumberOrAlphabet] == NO){
                return [RLHud msg:LS(@"密码只能包含数字和字母")];
            }
            [CCHttp post:api_register para:@{@"userName":self.input1.text,@"password":self.input2.text} success:^(id  _Nonnull response) {
                [RLHud startHud:LS(@"注册成功") stopTime:1.5 handle:^{
                    [self backBtnAction];
                }];
            } fail:^(NSError * _Nonnull error) { }];
        }
            break;
        case 202:///用户协议
        {
            SWPrivacyViewController *vc   = [SWPrivacyViewController new];
            vc.title = LS(@"隐私政策");
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 203:///返回登录
            [self backBtnAction];
            break;
        case 204:
            self.input2.secureTextEntry = !self.input2.secureTextEntry;
        default:
            break;
    }
}

@end
